#include "HudLayer.h"
#include "MainLayer.h"
#include "GamePlayScene.h"

USING_NS_CC;

HudLayer::HudLayer() :
	__pScoreLabel(NULL),
	__pShieldLabel(NULL),
	__pHealthLabel(NULL)
{
}

bool
HudLayer::init()
{
	if (!CCLayer::init())
	{
		return false;
	}

	// Ask director the window size.
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();

	// Create the score label.
    __pScoreLabel = CCLabelTTF::labelWithString("Score: 0", "Arial", 30);

	// Position the label on the right side of the screen.
	__pScoreLabel->setPosition(ccp(winSize.width - 100, winSize.height - 20));

	// Add the label as a child to this layer.
	this->addChild(__pScoreLabel, 1);

	__pShieldLabel = CCLabelTTF::labelWithString("Shield: 0", "Arial", 30);
	__pShieldLabel->setPosition(ccp(100, winSize.height - 20));
	this->addChild(__pShieldLabel, 1);

	__pHealthLabel = CCLabelTTF::labelWithString("Health: 0", "Arial", 30);
	__pHealthLabel->setPosition(ccp(100, winSize.height - 50));
	this->addChild(__pHealthLabel, 1);

	__pAutofireButton = (CCMenuItemToggle*) CCMenuItemToggle::itemWithTarget(this,
			 menu_selector(HudLayer::autofireCallback),
			 CCMenuItemFont::itemWithString("Autofire ON"),
			 CCMenuItemFont::itemWithString("Autofire OFF"),
			 NULL);
	__pAutofireButton->setPosition(ccp(100, 30));

	__pChangeGunButton = (CCMenuItemToggle*) CCMenuItemToggle::itemWithTarget(this,
			 menu_selector(HudLayer::changeGunCallback),
			 CCMenuItemFont::itemWithString("Standard"),
			 CCMenuItemFont::itemWithString("Bomb"),
			 NULL);
	__pChangeGunButton->setPosition(ccp(winSize.width - 100, 30));

	__pDrawDebugButton = (CCMenuItemToggle*) CCMenuItemToggle::itemWithTarget(this,
			 menu_selector(HudLayer::enableDrawDebug),
			 CCMenuItemFont::itemWithString("Draw debug OFF"),
			 CCMenuItemFont::itemWithString("Draw debug ON"),
			 NULL);
	__pDrawDebugButton->setPosition(ccp(100, 60));

	__pChangeGunButton = (CCMenuItemToggle*) CCMenuItemToggle::itemWithTarget(this,
			 menu_selector(HudLayer::changeGunCallback),
			 CCMenuItemFont::itemWithString("Standard"),
			 CCMenuItemFont::itemWithString("Bomb"),
			 NULL);
	__pChangeGunButton->setPosition(ccp(winSize.width - 100, 30));

	CCMenu* pMenu = CCMenu::menuWithItems(__pAutofireButton, __pChangeGunButton, __pDrawDebugButton, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

	return true;
}

void
HudLayer::changeGunCallback(cocos2d::CCObject* pSender)
{
	MainLayer* pMainLayer = static_cast<MainLayer*>(CCDirector::sharedDirector()->getRunningScene()->getChildByTag(GamePlayScene::mainLayerTag));
	pMainLayer->changeGun();
}

void
HudLayer::autofireCallback(cocos2d::CCObject* pSender)
{
	MainLayer* pMainLayer = static_cast<MainLayer*>(CCDirector::sharedDirector()->getRunningScene()->getChildByTag(GamePlayScene::mainLayerTag));
	pMainLayer->toggleAutofire();
}

void 
HudLayer::enableDrawDebug(cocos2d::CCObject* pSender)
{
	MainLayer* pMainLayer = static_cast<MainLayer*>(CCDirector::sharedDirector()->getRunningScene()->getChildByTag(GamePlayScene::mainLayerTag));
	pMainLayer->changeDrawDebugMode();
}
void
HudLayer::updateScore(int score)
{
	const int labelLength = 100;
	char scoreLabelText[labelLength];
	sprintf(scoreLabelText, "Score: %d", score);
	__pScoreLabel->setString(scoreLabelText);
}

void
HudLayer::updateHealth(int health)
{
	const int labelLength = 100;
	char scoreLabelText[labelLength];
	sprintf(scoreLabelText, "Health: %d", health);
	__pHealthLabel->setString(scoreLabelText);
}

void
HudLayer::updateShield(int shield)
{
	const int labelLength = 100;
	char scoreLabelText[labelLength];
	sprintf(scoreLabelText, "Shield: %d", shield);
	__pShieldLabel->setString(scoreLabelText);
}
